package br.com.fogas.medicao.application;

import java.sql.SQLException;

import br.com.fogas.database.FogasDatabase;
import br.com.fogas.database.entidade.Config;
import br.com.fogas.modelo.Configuracao;
import android.app.Application;
import android.content.Context;
import android.util.Log;

public class ApplicationFogas extends Application {
    
	private static ApplicationFogas instance;
	
    private Context context;
    
    private Configuracao configuracao;
    
    public static ApplicationFogas getInstance() {
    	if(instance == null) {
    		instance = new ApplicationFogas();
    	}
    	return instance;
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
        ApplicationFogas.getInstance().setContext(getApplicationContext());
        ApplicationFogas.getInstance().setConfiguracao();
    }
    
    public Context getContext() {
		return context;
	}
    
    public void setContext(Context context) {
		this.context = context;
	}
    
    public Configuracao getConfiguracao() {
		return configuracao;
	}
    
    public void setConfiguracao() {
    	try { 
			FogasDatabase database = new FogasDatabase(getContext());
			Config config = database.getConfig().queryForId(1);
			if(config != null) {
				this.configuracao = config.getConfiguration();
			}
			database.getHelper().close(); 
		} catch (SQLException e) {
			Log.e(getClass().getName(), e.getMessage());
		}
	}
}